package listlogmta;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub total_list_logmta {
    my $self = shift;

    my $sth = $self->{dbh}->prepare("SELECT count(*) FROM maillog");
    $sth->execute();
    $self->{p}->{total_number} = $sth->fetchrow_array();
    $sth->finish();

    return $self;
}

sub total_list_logmta_search {
    my $self = shift;

    my @search;
    @search = split( / /, $self->{p}->{search_logmta} );
    my $where;

    if ( $self->{p}->{s_type} eq '1' ) {
        $where = join( " OR ",
            map { "text LIKE " . $self->{dbh}->quote( '%' . $_ . '%' ) }
              @search );
    }
    elsif ( $self->{p}->{s_type} eq '2' ) {
        $where = join( " AND ",
            map { "text LIKE " . $self->{dbh}->quote( '%' . $_ . '%' ) }
              @search );
    }
    else {

        $where = "text LIKE "
          . $self->{dbh}->quote( '%' . $self->{p}->{search_logmta} . '%' );
    }

    $self->{SQL} = $where;

    my $sth =
      $self->{dbh}->prepare("SELECT count(*) FROM maillog where $where ");
    $sth->execute();
    $self->{p}->{total_number} = $sth->fetchrow_array();
    $sth->finish();


    $self->{p}->{url} =
        '&search_logmta='
      . $self->{p}->{search_logmta}
      . '&s_type='
      . $self->{p}->{s_type};

    return $self;
}

sub page_list_logmta {
    my $self = shift;

    $self->{p}->{file} = 'log';
    $self->SUPER::page_list(@_);
    return $self;
}

sub page_list_logmta_search {
    my $self = shift;
    $self->{p}->{file} = 'logsearch';
    $self->SUPER::page_list(@_);
    return $self;
}

sub return_list_logmta {
    my $self = shift;

    my $sth = $self->{dbh}->prepare(
        "SELECT data,
                                text
                         FROM maillog
                       ORDER BY id desc
                         LIMIT ?, ?

                             "
    );
    $sth->execute( $self->{p}->{jj}, $self->{se}->{line_from_page} );
    my $rows;
    push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    foreach $_ ( @{$rows} ) {
        $_->{text} =~ s/<//g;
        $_->{text} =~ s/>//g;

    }

    if ( !$rows ) {
        $self->{he}->{NOTS} = '1';
    }
    else {
        $self->{he}->{ROWS} = $rows;
    }

    return $self;
}

sub templ_hash_logmta_list {
    my $self = shift;

    $self->{session}->{filet} = 'log';

    $self->SUPER::templ_hash(@_);
}

sub return_list_logmta_search {
    my $self = shift;

    my $where = $self->{SQL};

    my $sth = $self->{dbh}->prepare(
        "SELECT *
                         FROM maillog
                         WHERE $where
                       ORDER BY id desc
                         LIMIT ?, ?
                             "
    );
    $sth->execute( $self->{p}->{jj}, $self->{se}->{line_from_page} );
    my $rows;
    push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    if ( !$rows ) {
        $self->{he}->{NOTS} = '1';
    }
    else {
        $self->{he}->{ROWS} = $rows;
    }

    return $self;
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
